package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdSensitiveImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    @Override
    public ResponseResult findpage(SensitiveDto sensitiveDto) {
        //1.如果从前端传过来的参数为空，则返回参数异常
        if (sensitiveDto==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数为空，请重新输入！");
        }
        //2.如果从前端传过来的数据不为空，先检查参数
        sensitiveDto.checkParam();
        //3.根据名称进行模糊分页查询
        Page page = new Page(sensitiveDto.getPage(), sensitiveDto.getSize());//创建查询的那个页面的主体框架
        //4.创建查询选择器
        LambdaQueryWrapper<AdSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(sensitiveDto.getName())) {
            lambdaQueryWrapper.like(AdSensitive::getSensitives,sensitiveDto.getName());
        }
        //5.将分页的形式和从数据库中查询的数据组合到一起
        IPage page1 = this.page(page, lambdaQueryWrapper);
        //6.将查询到的页面返回给前端页面
        PageResponseResult pageResponseResult = new PageResponseResult(sensitiveDto.getPage(), sensitiveDto.getSize(), page1.getTotal());
        pageResponseResult.setData(page1.getRecords());
        return pageResponseResult;// 将查询到的结果返回给前端页面
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //1.如果从前端传过来的参数为空，则返回给前端参数错误
        if (adSensitive==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"您输入的参数为空！");
        }
        //2.判断要添加的这条记录数据库中是否已经存在
        int count=this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives,adSensitive.getSensitives()));
        if (count>0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"数据已存在");
        }
        adSensitive.setCreatedTime(new Date());//设置其添加的时间
        save(adSensitive);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //1.判断如果前端传过来的参数为空，则报参数异常
        if (adSensitive==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);//参数
        }
        AdSensitive adSensitive1 = this.getById(adSensitive.getId());
        if (adSensitive1==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        updateById(adSensitive);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delete(Integer id) {
     //1.判断从前端传过来的id的值是否为空，若为空则响应参数错误
        if (id==null) {
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdSensitive sensitive = this.getById(id);
        if (sensitive==null) {
            ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST,"数据不存在！");
        }
        removeById(id);

        return ResponseResult.okResult();
    }
}
